package com.changanw.tienchin.web.controller.tienchin;



import com.changanw.tienchin.common.annotation.Log;
import com.changanw.tienchin.common.core.controller.BaseController;
import com.changanw.tienchin.common.core.domain.AjaxResult;
import com.changanw.tienchin.common.core.page.TableDataInfo;
import com.changanw.tienchin.common.enums.BusinessType;
import com.changanw.tienchin.common.utils.poi.ExcelUtil;
import com.changanw.tienchin.course.domain.Course;
import com.changanw.tienchin.course.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author changanw
 * @since 2023-11-23
 */
@RestController
@Controller
@RequestMapping("/tienchin/course")
public class CourseController extends BaseController {
  @Autowired
  ICourseService courseService;
  @PreAuthorize("hasPermission('tienchin:course:list')")
  @GetMapping("/list")
  public TableDataInfo list(Course course) {
    startPage();
    List<Course> list = courseService.selectCourseList(course);
    return getDataTable(list);
  }
  /**
   * 新增课程
   */
  @PreAuthorize("hasPermission('tienchin:course:create')")
  @Log(title = "课程管理", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@Validated @RequestBody Course course) {
    return courseService.addCourse(course);
  }
  /**
   * 修改课程
   */
  @PreAuthorize("hasPermission('tienchin:course:edit')")
  @Log(title = "课程管理", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@Validated @RequestBody Course course) {
    return courseService.updateCourse(course);
  }
  /**
   * 根据活动id获取 课程信息
   * @param channelId
   * @return
   */
  @PreAuthorize("hasPermission('tienchin:course:list')")
  @GetMapping("/{courseId}")
  public AjaxResult getInfo(@PathVariable Long courseId){
    return AjaxResult.success(courseService.getById(courseId));
  }
  /**
   * 删除课程
   */
  @PreAuthorize("hasPermission('tienchin:course:remove')")
  @Log(title = "课程管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{courseIds}")
  public AjaxResult remove(@PathVariable Long[] courseIds) {
    return toAjax(courseService.deleteCourseByIds(courseIds));
  }

  /**
   * 课程导出
   * @param response
   * @param role
   */
  @Log(title = "课程管理", businessType = BusinessType.EXPORT)
  @PreAuthorize("hasPermission('tienchin:course:export')")
  @PostMapping("/export")
  public void export(HttpServletResponse response, Course course) {
    List<Course> list = courseService.selectCourseList(course);
    ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
    util.exportExcel(response, list, "课程数据");
  }

}
